load("//bazel:macros.bzl", "exports_files_legacy")
load("@rules_cc//cc:defs.bzl", "cc_toolchain")
load(":linux_amd64_toolchain_config.bzl", "provide_linux_amd64_toolchain_config")
load(":mac_toolchain_config.bzl", "provide_mac_toolchain_config")

licenses(["notice"])

exports_files_legacy()

# https://bazel.build/docs/toolchains
# https://bazel.build/reference/be/platform#toolchain
toolchain(
    name = "clang_linux_x64_toolchain",
    # Where should we run this toolchain?
    exec_compatible_with = [
        "@platforms//os:linux",
        "@platforms//cpu:x86_64",
        # We do not want an extra constraint here related to the hermetic toolchain because
        # we want the toolchain to run on any Linux x64 machine (and it certainly can).
    ],
    # What can this toolchain build?
    target_compatible_with = [
        "@platforms//os:linux",
        "@platforms//cpu:x86_64",
        # We want to be able to explicitly tell Bazel to use this toolchain, and not the
        # default one on a user's machine or on the RBE worker. Thus we need an extra constraint
        # that we can use to differentiate the "stock" C++ toolchain from our hermetic one and
        # force that use by specifying the target platform.
        "//bazel/platform:use_hermetic_toolchain",
    ],
    toolchain = ":linux_amd64_host",
    # https://github.com/bazelbuild/rules_cc/blob/8bb0eb5c5ccd96b91753bb112096bb6993d16d13/cc/BUILD#L32-L36
    toolchain_type = "@rules_cc//cc:toolchain_type",
)

# Our one mac toolchain can run on either Intel Macs or M1 Macs, however Bazel does not allow you to specify
# more than one cpu type in exec_compatible_with. Thus, we list the same toolchain twice.
toolchain(
    name = "clang_mac_x64_toolchain",
    exec_compatible_with = [
        "@platforms//os:macos",
        "@platforms//cpu:x86_64",
    ],
    target_compatible_with = [
        "@platforms//os:macos",
        "@platforms//cpu:x86_64",
        "//bazel/platform:use_hermetic_toolchain",
    ],
    toolchain = ":mac_host",
    # https://github.com/bazelbuild/rules_cc/blob/8bb0eb5c5ccd96b91753bb112096bb6993d16d13/cc/BUILD#L32-L36
    toolchain_type = "@rules_cc//cc:toolchain_type",
)

toolchain(
    name = "clang_mac_arm64_toolchain",
    exec_compatible_with = [
        "@platforms//os:macos",
        "@platforms//cpu:arm64",
    ],
    target_compatible_with = [
        "@platforms//os:macos",
        "@platforms//cpu:arm64",
        "//bazel/platform:use_hermetic_toolchain",
    ],
    toolchain = ":mac_host",
    # https://github.com/bazelbuild/rules_cc/blob/8bb0eb5c5ccd96b91753bb112096bb6993d16d13/cc/BUILD#L32-L36
    toolchain_type = "@rules_cc//cc:toolchain_type",
)

filegroup(name = "not_implemented")

filegroup(
    name = "archive_linux_amd64_files",
    srcs = [
        "linux_trampolines/ar_trampoline_linux.sh",
        "@clang_linux_amd64//:archive_files",
    ],
)

filegroup(
    name = "compile_linux_amd64_files",
    srcs = [
        "linux_trampolines/IWYU_mapping.imp",
        "linux_trampolines/clang_trampoline_linux.sh",
        "@clang_linux_amd64//:compile_files",
    ],
)

filegroup(
    name = "link_linux_amd64_files",
    srcs = [
        # Bazel assumes it is talking to Clang when linking.
        "linux_trampolines/clang_trampoline_linux.sh",
        "@clang_linux_amd64//:link_files",
    ],
)

filegroup(
    name = "archive_mac_files",
    srcs = [
        "mac_trampolines/ar_trampoline_mac.sh",
        "@clang_mac//:archive_files",
    ],
)

filegroup(
    name = "compile_mac_files",
    srcs = [
        "mac_trampolines/clang_trampoline_mac.sh",
        "@clang_mac//:compile_files",
    ],
)

filegroup(
    name = "link_mac_files",
    srcs = [
        "mac_trampolines/clang_trampoline_mac.sh",
        "@clang_mac//:link_files",
    ],
)

provide_linux_amd64_toolchain_config(
    name = "linux_amd64_toolchain_config",
)

provide_mac_toolchain_config(
    name = "mac_toolchain_config",
)

# https://bazel.build/reference/be/c-cpp#cc_toolchain
cc_toolchain(
    name = "linux_amd64_host",
    all_files = ":not_implemented",
    ar_files = ":archive_linux_amd64_files",
    compiler_files = ":compile_linux_amd64_files",
    dwp_files = ":not_implemented",
    linker_files = ":link_linux_amd64_files",
    objcopy_files = ":not_implemented",
    strip_files = ":not_implemented",
    supports_param_files = False,
    toolchain_config = ":linux_amd64_toolchain_config",
)

cc_toolchain(
    name = "mac_host",
    all_files = ":compile_mac_files",  # Apparently also used to compile objc code
    ar_files = ":archive_mac_files",
    compiler_files = ":compile_mac_files",
    dwp_files = ":not_implemented",
    linker_files = ":link_mac_files",
    objcopy_files = ":not_implemented",
    strip_files = ":not_implemented",
    supports_param_files = False,
    toolchain_config = ":mac_toolchain_config",
)
